﻿using System;
using Microsoft.SharePoint;

namespace SPRepository
{
    public class UrlFieldType : SimpleFieldType<UrlFieldDto>
    {
        public UrlFieldType(Guid id) : base(id)
        {
        }

        public override UrlFieldDto GetValue(SPListItem item)
        {
            var fieldValue = GetFieldValue<SPFieldUrlValue>(item);
            if (fieldValue == null)
            {
                return null;
            }
            return new UrlFieldDto(fieldValue.Description, fieldValue.Url);
        }

        public override void SetValue(SPListItem item, UrlFieldDto value)
        {
            var fieldValue = GetFieldValue<SPFieldUrlValue>(item);
            if (value != null)
            {
                fieldValue.Url = value.Url;
                fieldValue.Description = value.Description;
            }
        }
    }
}